/*
 * Copyright 2010 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.mgwt.examples.uibinder.client;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleActivity;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleDisplay;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleListActivity;
import com.googlecode.mgwt.examples.uibinder.client.activities.ExampleSourceActivity;
import com.googlecode.mgwt.examples.uibinder.client.places.ExamplePlace;
import com.googlecode.mgwt.examples.uibinder.client.places.ExamplePlace.ID;
import com.googlecode.mgwt.examples.uibinder.client.places.ExampleSourcePlace;
import com.googlecode.mgwt.examples.uibinder.client.places.HomePlace;

/**
 * @author Daniel Kurka
 * 
 */
public class PhoneActivityMapper implements ActivityMapper {

	private final ClientFactory clientFactory;
	private DisplayMapper displayMapper;

	public PhoneActivityMapper(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		displayMapper = new DisplayMapper(this.clientFactory);
	}

	@Override
	public Activity getActivity(Place place) {
		if (place instanceof ExamplePlace) {
			ExampleDisplay display = displayMapper.getDisplay((ExamplePlace) place);
			if (display == null)
				return new ExampleListActivity(clientFactory);
			ID id = ID.UNKNOWN;
			if (place instanceof ExamplePlace) {
				ExamplePlace examplePlace = (ExamplePlace) place;
				id = examplePlace.getId();

			}

			if (place instanceof ExampleSourcePlace) {
				ExampleSourcePlace exampleSourcePlace = (ExampleSourcePlace) place;
				id = exampleSourcePlace.getId();
			}

			return new ExampleActivity(clientFactory, display, id);
		}

		if (place instanceof ExampleSourcePlace) {
			ExampleSourcePlace exampleSourcePlace = (ExampleSourcePlace) place;
			return new ExampleSourceActivity(clientFactory, exampleSourcePlace.getId());

		}

		if (place instanceof HomePlace) {
			return new ExampleListActivity(clientFactory);
		}
		return null;
	}
}
